package aip.assignment2.service;

import aip.assignment2.persistence.ProductCategoryDao;
import aip.assignment2.persistence.SqlParameter;
import aip.assignment2.persistence.entity.Product;
import aip.assignment2.persistence.entity.ProductCategory;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;

/**
 * This service class encapsulates functions for browsing products.
 */
@Stateless(name = "aip.assignment2.service.StationaryService")
@TransactionManagement(TransactionManagementType.CONTAINER)
public class ProductManager
{
	@EJB
	private ProductCategoryDao m_ProductCategoryDao;
	/**
	 * This method gets all product categories
	 * @return the result list of product categories
	 */
	public List<ProductCategory> getProductCategories()
	{
		return m_ProductCategoryDao.select();
	}
	/**
	 * This method gets product by its name.
	 * @param name the product name
	 * @return the result Product
	 */
	public Product getProductByName(String name)
	{
		if (name == null)
		{
			return null;
		}
		return m_ProductCategoryDao.selectSingleResultByNamedQuery("Product.findByName", new SqlParameter("name", name));
	}
	/**
	 * This method gets product category by its name.
	 * @param name the product category name
	 * @return the result ProductCategory
	 */
	public ProductCategory getProductCategoryByName(String name)
	{
		if (name == null)
		{
			return null;
		}
		return m_ProductCategoryDao.selectSingleResultByNamedQuery("ProductCategory.findByName", new SqlParameter("name", name));
	}
	/**
	 * This method gets all products of a specific category.
	 * @param productCategory the product category
	 * @return the result list of products
	 */
	public List<Product> getProductByCategory(ProductCategory productCategory)
	{
		if (productCategory == null)
		{
			return null;
		}
		return productCategory.getProductList();
	}
}
